import Router from 'koa-router';
import {files} from '../db/models.js'
import {getCollection} from '../chroma/index.js'
import {messageQueue} from '../utils/messageQueue.js'
const delete_knowledge_base_file = new Router();

delete_knowledge_base_file.post('/', async (ctx) => {
    try{
        const { knowledge_base, file_name } = ctx.request.body;
        for(const file of messageQueue){
            if(file.status === 'embedding'){
                if(file.knowledgeBase === knowledge_base && file.name === file_name){
                    ctx.status = 503
                    ctx.body = {
                        code: 503,
                        message: '不能删除正在embedding的文件！'
                    }
                    return
                }
                break
            }
        }
        const file = await files.findOne({ knowledgeBase: knowledge_base, name: file_name })
        const ids = file.ids;
        await getCollection(knowledge_base).delete({ ids });
        await file.remove()
        ctx.body = {
            code: 200,
            message: 'success'
        }
    }catch (e) {
        ctx.status = 500
        ctx.body = {
            code: 500,
            message: '删除失败'
        }
    }
})
export default delete_knowledge_base_file
